package com.hex.liyue.usermodule.presenter;

import android.text.TextUtils;

import com.hex.liyue.commonmodule.bean.BaseBean;
import com.hex.liyue.commonmodule.listener.EMLoginListener;
import com.hex.liyue.commonmodule.listener.UploadFileListener;
import com.hex.liyue.commonmodule.manager.HttpManager;
import com.hex.liyue.commonmodule.manager.UserManager;
import com.hex.liyue.commonmodule.net.ResponseListener;
import com.hex.liyue.commonmodule.utils.EMUtils;
import com.hex.liyue.commonmodule.utils.L;
import com.hex.liyue.commonmodule.utils.OssUtils;
import com.hex.liyue.commonmodule.utils.RegularUtils;
import com.hex.liyue.usermodule.bean.RegistResultBean;
import com.hyphenate.chat.EMClient;

/**
 * 这是一个注册页面的业务类
 * Created by yukoyuan on 16/6/14.
 */
public class RegistPresenter {
    IRegistVIew iRegistView;

    public RegistPresenter(IRegistVIew iRegistView) {
        this.iRegistView = iRegistView;
    }

    /**
     * 这是一个获取验证码的方法
     */
    public void getVcode() {
        //判断手机号是否正确
        if (!RegularUtils.phone(iRegistView.getPhone())) {
            iRegistView.showT("请输入正确的手机号");
            return;
        }
        //获取验证码
        iRegistView.delyTime(120);
        HttpManager.getInstance().getvcode(iRegistView.getPhone(), 1, new ResponseListener<BaseBean>() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                iRegistView.showT("发送成功");
            }

            @Override
            public void onFailure(String responseresult) {
                iRegistView.showT(responseresult);
            }
        });
    }

    /**
     * 这是一个注册的方法
     */
    public void regist() {
        //判断数据的正确性
        //头像是否为空
        if (iRegistView.getAvatar() == null) {
            iRegistView.showT("头像不能为空");
            return;
        }
        //姓名
        if (TextUtils.isEmpty(iRegistView.getUserName())) {
            iRegistView.showT("姓名不能为空");
            return;
        }
        //姓名字数的限制
        if (iRegistView.getUserName().length() > 15) {
            iRegistView.showT("姓名字数不能超过15字");
            return;
        }
        //手机号
        if (!RegularUtils.phone(iRegistView.getPhone())) {
            iRegistView.showT("请输入正确的手机号");
            return;
        }
        //验证码
        if (TextUtils.isEmpty(iRegistView.getVcode())) {
            iRegistView.showT("验证码不能为空");
            return;
        }
        //密码
        if (TextUtils.isEmpty(iRegistView.getPwd())) {
            iRegistView.showT("密码不能为空");
            return;
        }

        iRegistView.showLod("请求中...");
        if (iRegistView.getAvatar() != null && iRegistView.getAvatar().exists()) {
            //先传头像到阿里云
            String key = OssUtils.getInstance().objectKeyUserAvater + UserManager.getUserId() + "_" + iRegistView.getAvatar().getName();
            OssUtils.getInstance().uploadfile(key, iRegistView.getAvatar().getAbsolutePath(), new UploadFileListener() {
                @Override
                public void onSucess(String path) {
                    updateUserinfo(path);
                }

                @Override
                public void onFailure() {

                }

                @Override
                public void onProgress(long progress) {

                }
            });

        } else {
            //不更新头像
            updateUserinfo(null);
        }
    }

    private void updateUserinfo(String avater) {
        HttpManager.getInstance().regist(
                iRegistView.getPhone(),
                iRegistView.getUserName(),
                iRegistView.getPwd(),
                avater,
                iRegistView.getVcode(),
                "",
                new ResponseListener<RegistResultBean>() {
                    @Override
                    public void onSuccess(RegistResultBean registBean) {
                        L.e("registBean", registBean.toString());
                        UserManager.setUserId(registBean.data.hx_id);
                        loginEM(registBean.data.hx_id);
                    }

                    @Override
                    public void onFailure(String responseresult) {
                        iRegistView.showT(responseresult);
                        iRegistView.closeLod();
                    }
                }
        );

    }


    //登录环信的方法
    private void loginEM(String userName) {
        EMUtils.login(userName, new EMLoginListener() {
            @Override
            public void onSuccess() {
                iRegistView.showT("登录成功");
                EMClient.getInstance().groupManager().loadAllGroups();
                EMClient.getInstance().chatManager().loadAllConversations();
                L.d("main", "登录聊天服务器成功！");
                UserManager.setIsLogin(true);
                iRegistView.startMainActivity();

                iRegistView.closeLod();
            }

            @Override
            public void onFailure() {
                L.d("main", "登录聊天服务器失败！");
                iRegistView.showT("登录失败");

                iRegistView.closeLod();
            }
        });
    }
}
